package com.ds.infrastructure.hyperspace.container.internal.transport;

import com.ds.infrastructure.hyperspace.container.internal.HyperspaceThreadFactory;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.SocketChannel;
import lombok.extern.slf4j.Slf4j;

/**
 * @author  Weishaoying
 */
@Slf4j
public class NettyTransportFactoryDelegate implements NettyTransportFactory<EventLoopGroup> {

    private NettyTransportFactory delegate;

    public NettyTransportFactoryDelegate() {
        if (Epoll.isAvailable()) {
            delegate = new LinuxNativeTransportFactory();
            log.info("Hyperspace use [native epoll] transport.");
        } else {
            delegate = new JdkNioTransportFactory();
            log.info("Hyperspace use [jdk nio] transport.");
        }
    }

    @Override
    public Class<? extends SocketChannel> socketChannelClass() {
        return delegate.socketChannelClass();
    }

    @Override
    public Class<? extends ServerSocketChannel> serverSocketChannelClass() {
        return delegate.serverSocketChannelClass();
    }

    @Override
    public EventLoopGroup newEventLoopGroup(HyperspaceThreadFactory threadFactory, int threadNum) {
        return delegate.newEventLoopGroup(threadFactory, threadNum);
    }

    @Override
    public EventLoopGroup newAcceptorEventLoopGroup(HyperspaceThreadFactory threadFactory, int threadNum) {
        return delegate.newAcceptorEventLoopGroup(threadFactory, threadNum);
    }

}
